package com.huoyun.car.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.huoyun.base.action.BaseAction;
import com.huoyun.car.bean.Car;
import com.huoyun.car.model.CarPubInfoModel;
import com.huoyun.car.service.CarPubInfoService;
import com.huoyun.car.service.CarService;
import com.huoyun.car.service.DriverService;

public class PrePublishCarAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	private CarService carService;

	private String defaultContact;

	private int carId;

	private String loadMax;
	
	private String unit;
	
	private List<Car> carList;
	
	/**
	 * 管理司机的service
	 */
	private DriverService driverService;
	
	/**
	 * 当前用户的未上线车源信息列表
	 */
	private List<CarPubInfoModel> myOfflineCarPubInfoModelList;
	
	/**
	 * 管理车源信息的service
	 */
	private CarPubInfoService carPubInfoService;

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}

	public String getDefaultContact() {
		return defaultContact;
	}

	public void setDefaultContact() {		
		this.defaultContact = getLoginUser().getMobilePhone();
	}

	public int getCarId() {
		return carId;
	}

	public void setCarId(int carId) {
		this.carId = carId;
	}

	public String getLoadMax() {
		return loadMax;
	}

	public void setLoadMax(String loadMax) {
		this.loadMax = loadMax;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public List<Car> getCarList() {
		return carList;
	}

	public void setCarList(List<Car> carList) {
		this.carList = carList;
	}

	public DriverService getDriverService() {
		return driverService;
	}

	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}

	public List<CarPubInfoModel> getMyOfflineCarPubInfoModelList() {
		return myOfflineCarPubInfoModelList;
	}

	public void setMyOfflineCarPubInfoModelList(
			List<CarPubInfoModel> myOfflineCarPubInfoModelList) {
		this.myOfflineCarPubInfoModelList = myOfflineCarPubInfoModelList;
	}

	public CarPubInfoService getCarPubInfoService() {
		return carPubInfoService;
	}

	public void setCarPubInfoService(CarPubInfoService carPubInfoService) {
		this.carPubInfoService = carPubInfoService;
	}

	public String execute() {

		carList = carService.getUnPublishedCarListByUserId(getLoginUser().getId());
		
		myOfflineCarPubInfoModelList = carPubInfoService.getOfflineCarPubInfoModelList(getLoginUser().getId());
		
		loadMax = null;
		
		return SUCCESS;
	}

	public String selectCar() throws JSONException {
		
		Car car = carService.getCarById(this.carId);
		this.loadMax = car.getLoadMax();
		this.defaultContact = (driverService.getDriverById(car.getDriverId())).getMobilePhone();
		JSONObject jsonData = new JSONObject();
		jsonData.put("loadMax", this.loadMax);
		jsonData.put("defaultContact", this.defaultContact);
		jsonData.put("unit", this.unit);
		try {
			// 设置JSON编码为UTF-8
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setHeader("Cache-Control", "no-cache");
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().println(jsonData);

			// // 设置"companyName"为session的Attribute
			// ServletActionContext.getRequest().getSession()
			// .setAttribute("companyName", companyname);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
